<?php
/**
 * 商品控制器
 * User: Administrator
 * Date: 2019/3/12 0012
 * Time: 下午 13:32
 */

namespace app\admin\controller;
use cmf\controller\AdminBaseController;
use think\Db;

class GoodsController extends AdminBaseController{

    public function initialize()
    {
        $adminSettings = cmf_get_option('admin_settings');
        if (empty($adminSettings['admin_password']) || $this->request->path() == $adminSettings['admin_password']) {
            $adminId = cmf_get_current_admin_id();
            if (empty($adminId)) {
                session("__LOGIN_BY_CMF_ADMIN_PW__", 1);//设置后台登录加密码
            }
        }

        parent::initialize();
    }

    function index(){
        $goodsLists = Db::name('goods')->order('goods_id desc')->limit(1)->select();
        $this->assign('goodsLists',$goodsLists);
        return $this->fetch();
    }

    /**
     * 修改商品状态
     */
    function changeStatus(){
        $status = $this->request->param('status');
        $goods_id = $this->request->param('goods_id',0,'intval');
        if(!$status){
            $this->ApiResult(false,10001,'参数传入错误');
        }
        $rs = Db::name('goods')->where('goods_id',$goods_id)->setField('goods_status',$status);
        if($rs){
            $this->ApiResult(true,10000);
        }else{
            $this->ApiResult(false,10001,'操作失败');
        }
    }

    /**
     * 修改
     */
    function edit(){
        $goods_id = $this->request->param('id',0,'intval');
        if(!$goods_id){
            $this->error('参数传入错误');
        }
        $goods = Db::name('goods')->where('goods_id',$goods_id)->find();
        $this->assign('goods',$goods);
        return $this->fetch();
    }

    /**
     * 提交修改
     */
    function editPost(){
        $goods_id = $this->request->param('goods_id');
        $goods_stock = $this->request->param('goods_stock');
        if(!$goods_id){
            $this->error('参数传入失败');
        }
        $rs = Db::name('goods')->where('goods_id',$goods_id)->setField('goods_stock',$goods_stock);
        if($rs){
            $this->success('保存成功');
        }else{
            $this->error('保存失败');
        }
    }

}